package com.yushu.authority.controller;

import com.yushu.authority.pojo.vo.PmOrgVO;
import com.yushu.authority.pojo.bo.PmOrg;
import com.yushu.authority.service.PmOrgService;

import com.yushu.common.anno.LogAop;
import com.yushu.common.execption.CgException;
import com.yushu.common.util.pager.Pages;
import com.yushu.common.util.result.Result;
import org.apache.commons.lang3.StringUtils;

//import io.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


 /**
 * @author test
 * @CreateDate 2020-12-03 15:47:25
 */
//@Api(description = "组织机构管理")
@Controller
@RequestMapping("/authority/pmOrg")
public class PmOrgController {
    private Logger logger = Logger.getLogger(PmOrgController.class);

    @Autowired
    private PmOrgService pmOrgService;
    private Map<String, Object> resultMap;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }

    /**
     * 分页条件查询组织机构数据
     * 2020-01-03 12:24:37
     *
     * @param tablePage
     * @return
     */
    @LogAop("分页查询组织机构")
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Pages page(@RequestBody Pages<PmOrgVO> tablePage) {

        Pages page = pmOrgService.page(tablePage);
        return page;
    }

    /**
     * 查询全部组织机构数据
     * 2020-01-03 12:24:37
     */
    @ResponseBody
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public List<PmOrg> queryList(@RequestBody PmOrg pmOrg) {
        List<PmOrg> list = pmOrgService.selectList(pmOrg);
        System.out.println("执行queryList+PmOrg");
        System.out.println("全部组织机构数据:"+list.toString());
        return list;
    }

    /**
     * 添加或者更新组织机构
     * 2020-01-03 12:24:37
     *
     * @param pmOrg
     * @return
     */
    @LogAop("新增组织机构")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody PmOrg pmOrg) {
        try {
            logger.info("进入到添加或修改组织机构方法");
            pmOrgService.insert(pmOrg);
            return Result.success("组织机构新增成功");

        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
    
     @LogAop("修改组织机构")
     @RequestMapping(value = "/editor", method = RequestMethod.POST)
     @ResponseBody
     public Result editor(@RequestBody PmOrg pmOrg) {
         try {
             logger.info("进入到添加或修改组织机构方法");
             pmOrgService.updateByKey(pmOrg);
             return Result.success("组织机构修改成功");
         }catch (Exception e){
             e.printStackTrace();
             return Result.error(e.getMessage());
         }
     }

    /**
     * 删除组织机构数据
     * 2020-01-03 12:24:37
     *
     * @param pmOrg 需要删除的对象
     */
    @LogAop("删除组织机构")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(Model model, @RequestBody PmOrg pmOrg) {
        try {
            logger.info("进入到删除组织机构方法");
            pmOrgService.deleteById(pmOrg.getId());
            return Result.success("删除组织机构成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
//
    }

    /**
     * 批量删除组织机构数据
     * 2020-01-03 12:24:37
     *
     * @param pmOrgs 组织机构集合
     */
    @LogAop("批量删除组织机构")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public Result batchDelete(Model model, @RequestBody List<PmOrg> pmOrgs) {
        try {
            pmOrgService.deleteBatch(pmOrgs);
            logger.info("进入到删除组织机构方法");
            return Result.success("批量删除组织机构成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());

        }
    }

//    /**
//     * 跳转组织机构列表页面
//     * 2020-01-03 12:24:37
//     */
//    @RequestMapping(value = "toPmOrgList", method = RequestMethod.GET)
//    public String toPmOrgList(Model model) {
//        model.addAttribute("title", "组织机构管理");
//        model.addAttribute("page", "/authority/pmOrg/list");
//        return "menuPage";
//    }

//    /**
//     * 跳转组织机构跳转到空白列表页面
//     * 2020-01-03 12:24:37
//     */
//    @RequestMapping(value = "toOpenPmOrgList", method = RequestMethod.GET)
//    public String toOpenPmOrgList(Model model, HttpServletRequest request) {
//        model.addAttribute("title", "组织机构管理");
//        model.addAttribute("page", "/authority/pmOrg/list");
//        return "openPage";
//    }
//
//    /**
//     * 跳转组织机构新增或修改页面
//     * 2020-01-03 12:24:37
//     */
//    @RequestMapping(value = "toEditor", method = RequestMethod.GET)
//    public String toEditor(Model model, String id, HttpServletRequest request) {
//        if (StringUtils.isNotEmpty(id)) {
//            request.setAttribute("data", pmOrgService.queryPmOrgByKey(id));
//        }
//        model.addAttribute("title", "组织机构管理");
//        model.addAttribute("page", "/authority/pmOrg/addUpdate");
//        return "openPage";
//    }

    @RequestMapping("checkRepeat")
    @ResponseBody
    public String checkRepeat(@RequestBody Map map) {
        return pmOrgService.checkRepeat(map);
    }
    
    /*校验机构名称是否重复*/
     @ResponseBody
     @RequestMapping(value = "/validateOrgName", method = RequestMethod.POST)
     public String validateOrgName(String orgName, String key) {
         try {
             return pmOrgService.validateOrgName(orgName, key);
         } catch (Exception e) {
             e.printStackTrace();
             return "false";
         }
     }
    
}

